package com.student.service.impl;

import com.student.bean.HealthyInfo;
import com.student.mapper.HealthyInfoMapper;
import com.student.service.HealthInfoService;
import com.student.utils.AjaxResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class HealthInfoServiceImpl implements HealthInfoService {
    @Resource
    private HealthyInfoMapper HealthInfoMapper;

    /**
     * 健康信息录入
     * @param time
     * @param height
     * @param weight
     * @param diet
     * @param BP
     * @param SQ
     * @return
     */
    @Override
    public AjaxResult insertHealthInfo(String time, Float height, Float weight, String diet, Float BP, String SQ) {
        HealthyInfo HealthInfo = new HealthyInfo();

        try {
            Date dateTime = StringToDate(time);
            HealthInfo.setAddDate(dateTime);
            HealthInfo.setHeight(height);
            HealthInfo.setWeight(weight);
            HealthInfo.setDietquality(diet);
            HealthInfo.setSleepquality(SQ);
            HealthInfo.setBp(BP);
            //存入数据库
            Integer flag = HealthInfoMapper.insert(HealthInfo);
            System.out.println("保存了："+flag);

        } catch (ParseException e) {
            e.printStackTrace();
            return AjaxResult.getNO("录入失败");
        }
        return AjaxResult.getOK("录入成功");
    }

    /**
     * 按月份查询健康信息数据
     * @return
     */
    @Override
    public AjaxResult selectHealthInfo(@Param(value = "month") String month) {
        System.out.println("匹配结果："+month+"%");
        System.out.println(HealthInfoMapper.selectHealthInfo(month+"%").toString());
        return AjaxResult.getOK(HealthInfoMapper.selectHealthInfo(month+"%"));
    }

    //查 id
    @Override
    public AjaxResult selectInfo(Integer health_id) {

        return AjaxResult.getOK(HealthInfoMapper.selectByPrimaryKey(health_id));
    }

    //字符串转Date
    private Date StringToDate(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return sdf.parse(time);
    }
}
